package com.cqut.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSONObject;
import com.cqut.dto.LimitShowDTO;
import com.cqut.dto.NoticeManagerDTO;
import com.cqut.model.ProblemManager;
import com.cqut.pojo.vo.noticeInfoVO;
import com.cqut.pojo.vo.problemInfoVO;
import com.cqut.dto.ProblemManagerDTO;
import com.cqut.util.JSONUtil;
import com.cqut.util.ResponseUtil;
import com.cqut.service.IProblemManagerService;


@RestController
@RequestMapping("/api/problemManager")
public class ProblemManagerController {
    @Resource(name="problemManagerService")
    private IProblemManagerService problemManagerService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponseEntity<String> list(
        @Validated
        LimitShowDTO limitShowDTO, BindingResult errors) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();

        Map<String, Object> result = new HashMap<String, Object>();

        List<Map<String, Object>> problemManagerList = problemManagerService.loadMaps(null, null, null, null, limitShowDTO.getCurPage(), limitShowDTO.getLimit());
        long count = problemManagerService.count();

        result.put("rows", problemManagerList);
        result.put("total", count);

        return ResponseUtil.getResEntityForGetAndJson(result);
    }
    
    /**
     * 初始化列表
     * @param limitShowDTO
     * @param errors
     * @return
     */
    @RequestMapping(value = "/problemList", method = RequestMethod.GET)
    public ResponseEntity<String> noticeList(
    	ProblemManagerDTO problemManagerDTO, BindingResult uErrors,
        @Validated
        LimitShowDTO limitShowDTO, BindingResult errors) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();

        List<problemInfoVO>  result = problemManagerService.loadProblemList(problemManagerDTO, limitShowDTO);

        return ResponseUtil.getResEntityForGet(JSONUtil.toJSONArrayForJavaObject(result).toJSONString());
    }
    
    /**
     * 根据搜索条件进行筛选列表数量
     */
    @RequestMapping(value="/count", method=RequestMethod.GET)
	public ResponseEntity<String> findProblemsWithLimit(
			ProblemManagerDTO problemManagerDTO, BindingResult uErrors) {
    	if (uErrors.hasErrors())
			ResponseUtil.getResponseEntityWhenInvalidReqParams();
		JSONObject json = new JSONObject();
		json.put("totalItems", problemManagerService.findCountOfProblems(problemManagerDTO));
		
		return ResponseUtil.getResEntityForGet(json.toJSONString());
	}
    

    @RequestMapping(value = "/detail/{problemId}", method = RequestMethod.GET)
    public ResponseEntity<String> show(@PathVariable("problemId") String problemId,
		@Validated
		ProblemManagerDTO problemManagerDTO, BindingResult errors) {
        Map<String, Object> problemManagerModel = problemManagerService.findMap(problemId);

        return ResponseUtil.getResEntityForGetAndJson(problemManagerModel);
    }

    @RequestMapping(value = "/problemAdd", method = RequestMethod.POST)
    public ResponseEntity<String> create(
        @Validated
        ProblemManagerDTO problemManagerDTO, BindingResult errors) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();

        ProblemManager problemManagerModel = problemManagerDTO.toModel();

        return ResponseUtil.getResEntityForPPP(problemManagerService.save(problemManagerModel));
    }

    @RequestMapping(value = "/problemUpdate", method = RequestMethod.PUT)
    public ResponseEntity<String> update(
        @Validated
        ProblemManagerDTO problemManagerDTO, BindingResult errors) {

        ProblemManager problemManagerModel = problemManagerDTO.toModel();
        problemManagerModel.setProblemId(problemManagerDTO.getProblemId());
        return ResponseUtil.getResEntityForPPP(problemManagerService.update(problemManagerModel));
    }

    @RequestMapping(value = "/{problemId}", method = RequestMethod.DELETE)
    public ResponseEntity<String> destory(@PathVariable("problemId") String problemId) {
        return ResponseUtil.getResEntityForDel(problemManagerService.delete(problemId));
    }

    @RequestMapping(value = "/problemDel", method = RequestMethod.POST)
    public ResponseEntity<String> deletes(@RequestParam("primaryKeys") String[] primaryKeys) {
        return ResponseUtil.getResEntityForGet(problemManagerService.batchDelete(primaryKeys));
    }
}
